package pure.engine.model.sections 
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.system.ApplicationDomain;
	import pure.engine.core.SystemManager;
	
	import pure.engine.model.RenderUtil;
	import pure.engine.core.ns_despair;
	
	use namespace ns_despair;
	
	/**
	 * 
	 * @author Pure
	 * 
	 */
internal final class MovieClipSection extends Section
{
	public function MovieClipSection(name:String, source:*, x:Number, y:Number, realRect:Rectangle)
	{
		super(name, source, x, y, realRect);

	}

	
	protected var _swf:MovieClip;



	override protected function _parseAssets(source:*):void
	{
		var obj:Class;
		
		if (source is String)
		{
			obj = SystemManager.getDefinition(source);
			if(!obj)
			{
				throw new Error('[MovieClipSection] - ＃_parseAssets - 资源 (' + source + ') 不存在.');
			}
			_swf = new obj();
		}
		
		else
		{
			_swf = source;
		}
		
		_swf.stop();
		
		m_width      =   _swf.width;
		m_height     =   _swf.height;
		m_maxLength  =   _swf.totalFrames * 2 + 1;
		m_bitmaps    =   new Vector.<BitmapData>(m_maxLength, true);
	}
	

	override protected function _preDraw(pointer:int):void
	{
		var bitmapData:BitmapData;
		var tmpPointer:int, frame:int;
		
		// 正向绘制
		if (pointer > 0) 
		{
			_swf.gotoAndStop(pointer);
			m_bitmaps[pointer] = _createBitmapByDisplay(_swf, m_width,m_height, false);
		}
		// 反向绘制
		else if (pointer < 0)
		{
			tmpPointer = m_maxLength + pointer;
			frame = ~pointer + 1;
			_swf.gotoAndStop(frame);
			m_bitmaps[pointer] = _createBitmapByDisplay(_swf, m_width,m_height, true);
		}
	
		else 
		{
			throw new Error(" 片段指针不可为0");
		}
	}
	
}

}